// https://leetcode.cn/problems/max-consecutive-ones-iii/description/
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int n = nums.size();
        int cnt = 0;
        int ret = -1;
        for(int i = 0, j = 0; j < n; j++) {
            if(nums[j] == 0) {
                cnt++;
            }
            while(cnt > k) {
                if(nums[i++] == 0) {
                    cnt--;
                }
            }
            ret = max(ret, j - i + 1);
        }
        return ret == -1 ? 0 : ret;
    }
};